package com.enjoy.leo_recyclerview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 自定义分割线
 */
public class StarDecoration extends RecyclerView.ItemDecoration {

    private int groupHeaderHeight;

    private Paint headPaint;
    private Paint textPaint;

    private Rect textRect;

    public StarDecoration(Context context) {
        groupHeaderHeight = dp2px(context, 100);

        headPaint = new Paint();
        headPaint.setColor(Color.RED);

        textPaint = new Paint();
        textPaint.setTextSize(50);
        textPaint.setColor(Color.WHITE);

        textRect = new Rect();
    }

    //此方法也是不断的调用,因为跟着item绘制
    @Override
    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.onDraw(c, parent, state);
        if (parent.getAdapter() instanceof StarAdapter) {
            StarAdapter adapter = (StarAdapter) parent.getAdapter();
            // 当前屏幕的item个数
            int count = parent.getChildCount();
            int left = parent.getPaddingLeft();
            int right = parent.getWidth() - parent.getPaddingRight();
            for (int i = 0; i < count; i++) {
                // 获取对应i的View
                View view = parent.getChildAt(i);
                // 获取View的布局位置
                int position = parent.getChildLayoutPosition(view);
                // 是否是头部
                boolean isGroupHeader = adapter.isGourpHeader(position);
                /*view.getTop() - groupHeaderHeight - parent.getPaddingTop()>=0,表示recycleView的头部Item在自定义头部分割线的下方
                * groupHeaderHeight + parent.getPaddingTop()表示自定义分割线头部的底部坐标
                *
                * */
                if (isGroupHeader && view.getTop() - groupHeaderHeight - parent.getPaddingTop() >= 0) {
                    Log.d(getClass().getSimpleName(),view.getTop() - groupHeaderHeight - parent.getPaddingTop()+"onDraw");
                    c.drawRect(left, view.getTop() - groupHeaderHeight, right, view.getTop(), headPaint);
                    String groupName = adapter.getGroupName(position);

                    //用画笔textPaint写groupName文字,得到的最小矩形,赋值给textRect
                    textPaint.getTextBounds(groupName, 0, groupName.length(), textRect);


                    /*
                    *要描绘到文字中心,基线公式view的高度/2-文字绘制最小高度/2
                    *那么view.getTop() -groupHeaderHeight / 2就表示自己分割条目的中心坐标
                    * textRect.height() / 2就表示文字绘制最小高度/2
                    * */

                    c.drawText(groupName, left + 20, view.getTop() -
                            groupHeaderHeight / 2 + textRect.height() / 2, textPaint);

                } else if (view.getTop() - groupHeaderHeight - parent.getPaddingTop() >= 0) {
                    // 分割线
                    c.drawRect(left, view.getTop() - 4, right, view.getTop(), headPaint);
                }
            }
        }
    }

    //此方法也是不断的调用,因为跟着item绘制
    @Override
    public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
        if (parent.getAdapter() instanceof StarAdapter) {
            StarAdapter adapter = (StarAdapter) parent.getAdapter();
            // 返回可见区域内的第一个item的position
            int position = ((LinearLayoutManager) parent.getLayoutManager()).findFirstVisibleItemPosition();
            // 获取对应position的View
            View itemView = parent.findViewHolderForAdapterPosition(position).itemView;
            int left = parent.getPaddingLeft();
            int right = parent.getWidth() - parent.getPaddingRight();
            int top = parent.getPaddingTop();
            // 当第二个是组的头部的时候,此时第一个会被我们画的顶遮住,但是它对于recycleView还是第一个可见的
            boolean isGroupHeader = adapter.isGourpHeader(position + 1);
            if (isGroupHeader) {
                int itemHeight = Math.min(groupHeaderHeight, itemView.getBottom() - parent.getPaddingTop());//得到滑动的item界面上的实时高度
                Log.d(getClass().getSimpleName(),"left:"+left+" right:"+right+" top:"+top+" bottom:"+ (itemView.getBottom() - parent.getPaddingTop()));
                c.drawRect(left, top, right, top + itemHeight, headPaint);//在画布上画一个矩形,即item实时高度的矩形
                String groupName = adapter.getGroupName(position);
                textPaint.getTextBounds(groupName, 0, groupName.length(), textRect);//求文字大小
                //此处基线也是设置为绘制的矩形中心点再往下移动文字高度的一半
                c.drawText(groupName, left + 20, top + itemHeight
                            - groupHeaderHeight / 2 + textRect.height() / 2, textPaint);//在这个矩形区域上,绘制文字
            } else {
                c.drawRect(left, top, right, top + groupHeaderHeight, headPaint);
                String groupName = adapter.getGroupName(position);
                textPaint.getTextBounds(groupName, 0, groupName.length(), textRect);

                /*
                *top + groupHeaderHeight - groupHeaderHeight / 2 + textRect.height() / 2
                *
                * top + groupHeaderHeight为这个矩形区域底部坐标
                * top + groupHeaderHeight - groupHeaderHeight / 2 为这个矩形的中间点y轴坐标
                * top + groupHeaderHeight - groupHeaderHeight / 2 + textRect.height() / 2 ,设置基线的距离矩形中心点再往下移动文字高度的一半
                *
                */
                c.drawText(groupName, left + 20, top + groupHeaderHeight / 2 + textRect.height() / 2, textPaint);
            }
        }
    }

    /**
     * 此方法设置分割线的区域
     * @param outRect
     * @param view
     * @param parent
     * @param state
     */
    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);

        if (parent.getAdapter() instanceof StarAdapter) {
            StarAdapter adapter = (StarAdapter) parent.getAdapter();
            int position = parent.getChildLayoutPosition(view);
            boolean isGroupHeader = adapter.isGourpHeader(position);
            // 怎么判断 itemView是头部
            if (isGroupHeader) {
                // 如果是头部，预留更大的地方,设置区域
                outRect.set(0, groupHeaderHeight, 0, 0);
            } else {
                // 1像素,设置区域
                outRect.set(0, 4, 0, 0);
            }
        }
    }

    private int dp2px(Context context, float dpValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale * 0.5f);
    }
}
